//
//  CWSportMapViewController.m
//  China_Walk
//
//  Created by HYY on 16/10/21.
//  Copyright © 2016年 qishuolei. All rights reserved.
//

#import "CWSportMapViewController.h"
#import <MAMapKit/MAMapKit.h>

@interface CWSportMapViewController ()<MAMapViewDelegate>

@end

@implementation CWSportMapViewController
    {
        CLLocation *_startLocation;
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupMap];
}

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    if (!updatingLocation) {
        return;
    }
    if (_startLocation == nil) {
//        实例化大头针
        MAPointAnnotation *annotation = [MAPointAnnotation new];
//        指定坐标位置
        annotation.coordinate = userLocation.location.coordinate;
//        添加到地图视图
        [mapView addAnnotation:annotation];
    }
    
}

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
//    判断大头针类型
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        nil;
    }
//    查询可重用大头针视图
    static NSString *annotationID = @"annotationId";
    MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationID];
//    如果没有查到，新建视图
    if (annotationView == nil) {
        annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
    }
//    设置大头针视图  设置头像
    UIImage *image = self.tracking.sportImage;
    annotationView.image = image;
    annotationView.centerOffset = CGPointMake(0, -image.size.height* 0.5);
    return annotationView;
    
    
}

-(void)setupMap{
//   1 实例化地图视图
    MAMapView *mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
//    2 添加到跟视图
    [self.view addSubview:mapView];
//    3 隐藏比例尺
    mapView.showsScale = NO;
//    4 关闭相机 省电
    mapView.rotateCameraEnabled = NO;
//    5 显示用户位置
    mapView.showsUserLocation = YES;
//    6 跟踪用户位置  可以将用户定位在地图中心 但是有些时候会比较慢
    mapView.userTrackingMode = MAUserTrackingModeFollow;
//    7 允许后台定位
    mapView.allowsBackgroundLocationUpdates = YES;
//    8 不允许系统暂停位置更新
    mapView.pausesLocationUpdatesAutomatically = NO;
    mapView.delegate = self;
    
}


@end
